{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "378fadf8d5274c12bb21d304d7ec0d5b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, fps_meter=False, grid=[-1, -1, -1, 1, 1, 1]…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\" This demo solves the Stokes equations, using quadratic elements for\n",
    "the velocity and first degree elements for the pressure (Taylor-Hood elements).\"\"\"\n",
    "# Credits: https://github.com/pf4d/fenics_scripts/blob/master/cbc_block/stokes.py\n",
    "from dolfin import *\n",
    "from time import time\n",
    "from vtkplotter.dolfin import plot, datadir\n",
    "\n",
    "# Load mesh and subdomains\n",
    "mesh = Mesh(datadir+\"dolfin_fine.xml\")\n",
    "sub_domains = MeshFunction(\"size_t\", mesh, datadir+\"dolfin_fine_subdomains.xml.gz\")\n",
    "\n",
    "# Define function spaces\n",
    "P2 = VectorElement(\"Lagrange\", mesh.ufl_cell(), 2)\n",
    "P1 = FiniteElement(\"Lagrange\", mesh.ufl_cell(), 1)\n",
    "TH = P2 * P1\n",
    "W = FunctionSpace(mesh, TH)\n",
    "\n",
    "# No-slip boundary condition for velocity\n",
    "noslip = Constant((0, 0))\n",
    "bc0 = DirichletBC(W.sub(0), noslip, sub_domains, 0)\n",
    "\n",
    "# Inflow boundary condition for velocity\n",
    "inflow = Expression((\"-sin(x[1]*pi)\", \"0.0\"), degree=2)\n",
    "bc1 = DirichletBC(W.sub(0), inflow, sub_domains, 1)\n",
    "bcs = [bc0, bc1]\n",
    "\n",
    "# Define variational problem\n",
    "(u, p) = TrialFunctions(W)\n",
    "(v, q) = TestFunctions(W)\n",
    "f = Constant((0, 0))\n",
    "a = (inner(grad(u), grad(v)) - div(v)*p + q*div(u))*dx\n",
    "L = inner(f, v)*dx\n",
    "w = Function(W)\n",
    "\n",
    "solve(a == L, w, bcs) #solver_parameters={'linear_solver':'mumps'}\n",
    "(u, p) = w.split() # Split the mixed solution using a shallow copy\n",
    "\n",
    "######################################################## vtkplotter:\n",
    "plot(u, mode='mesh and arrows', warpZfactor=-0.05, scale=0.02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
